
/*

Design Pattern Automation Toolkit.
Application to create applications with emphasis on Design patterns.
And support for round trip engineering.
Copyright (C) 2004 Vineeth Neelakant. nvineeth@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
using System;
using System.Reflection;
using System.Collections;
using DesignPatternDefinitions;

namespace DotNetReverseEngineering
{
	/// <summary>
	/// implemnts IClass
	/// </summary>
	public class RevEnggClassInfo : IClass
	{
		private Type classType;
		private Hashtable metaInfo;
		private ArrayList methods = new ArrayList();
		private ArrayList fields = new ArrayList();
		private ArrayList inheritedClasses = new ArrayList();
		private string componentName=DotNetReverseEngineer.DefaultNamespace;
		public static string csharp="C#";
		

		public RevEnggClassInfo(Type t)
		{

			this.classType = t;
			metaInfo=new Hashtable();
			
			this.InitAggregates();
			this.InitInheritance();
			this.InitFields();
			this.InitMethods();
			this.InitMetaInfo();
			this.ComponentName = this.classType.Namespace;
			
		}
		
		#region Init Methods
		private void InitMethods()
		{
			MethodInfo[] mthdInfo = this.classType.GetMethods(
					BindingFlags.Static | BindingFlags.Public |
					BindingFlags.Instance |
					BindingFlags.NonPublic );
				foreach( MethodInfo mthd in mthdInfo)
				{
					//consider only the methods of the current class not hte inherited methods...
					if( mthd.DeclaringType.Name.Equals(this.classType.Name) )
					{
						IMethod method = new RevEnggMethodInfo(mthd);
						methods.Add(method);
					}
				}
		}
		
		private void InitFields()
		{
			FieldInfo[] fldInfo = this.classType.GetFields(
					BindingFlags.Public| BindingFlags.NonPublic|
					BindingFlags.Static| BindingFlags.Instance
					);
				foreach( FieldInfo fld in fldInfo)
				{
					if(!fld.FieldType.IsByRef)
					{
						Console.WriteLine("FieldType is "+ fld.FieldType.Name);
						IField field = new RevEnggFieldInfo(fld);
						fields.Add(field);
					}
					else
					{
						//add it to the aggregate list.
					}
				}
		}
		
		private void InitInheritance()
		{
			Type[] inters = this.classType.GetInterfaces();
			foreach( Type inter in inters )
			{
				IInheritance inh = new RevEnggInheritanceInfo(this.classType,inter);
				this.inheritedClasses.Add(inh);
			}
			Type btype = this.classType.BaseType;
			if(btype!=null)
			this.inheritedClasses.Add (
			                           new RevEnggInheritanceInfo
			                           (this.classType,btype)
			                          );
				
		}
		
		private void InitAggregates()
		{
		
		}
		
		private void InitMetaInfo()
		{
			string minfo="";
			if(this.classType.IsInterface) minfo+="interface";
			if(this.classType.IsEnum) minfo+=" "+ "enum";
			if(this.classType.IsSealed) minfo+=" "+"sealed";
			if(this.classType.IsSerializable) minfo+=" "+"serializable";
			this.metaInfo.Add(RevEnggClassInfo.csharp,minfo);
		}
		#endregion 		
		#region IClass Members
		public Hashtable GetAllMetaInfo()
		{
			return this.metaInfo;
		}
		public System.Drawing.Point UILocation
		{
			get
			{
				return new System.Drawing.Point();
			}
			set
			{}
		}

		public System.Collections.ArrayList Fields
		{
			get
			{
				return fields;
			}
			set
			{}
		}
		
		public System.Collections.ArrayList Methods
		{
			get
			{
				return methods;
			}
			set
			{}
		}

		public System.Collections.ArrayList InheritedClasses
		{
			get
			{
				return this.inheritedClasses;
			}
			set
			{}
		}

		public DesignPatternDefinitions.ClassType ClassType
		{
			get
			{
				if(this.classType.IsAbstract) return DesignPatternDefinitions.ClassType.Abstract;
				return DesignPatternDefinitions.ClassType.Concrete;
			}
			set
			{}
		}

		public string QualifiedClassName
		{
			get
			{
				return this.classType.Namespace+"::"+this.classType.Name;
			}
			set
			{}
		}

		public System.Collections.ArrayList Creations
		{
			get
			{
				// TODO:  Add RevEnggClassInfo.Creations getter implementation
				return null;
			}
			set
			{
				// TODO:  Add RevEnggClassInfo.Creations setter implementation
			}
		}

		public string AccessModifier
		{
			get
			{
				// TODO:  Add RevEnggClassInfo.AccessModifier getter implementation
				return null;
			}
			set
			{
				// TODO:  Add RevEnggClassInfo.AccessModifier setter implementation
			}
		}

		public System.Collections.ArrayList Aggregates
		{
			get
			{
				// TODO:  Add RevEnggClassInfo.Aggregates getter implementation
				return null;
			}
			set
			{
				// TODO:  Add RevEnggClassInfo.Aggregates setter implementation
			}
		}

		

		public string GetMetaInfo(string language)
		{
			// TODO:  Add RevEnggClassInfo.GetMetaInfo implementation
			return null;
		}

		public System.Collections.ArrayList References
		{
			get
			{
				// TODO:  Add RevEnggClassInfo.References getter implementation
				return null;
			}
			set
			{}
		}

		public string ClassName
		{
			get
			{
				return this.classType.Name;
			}
			set{}
		}

		public string ComponentName
		{
			get
			{
				return this.componentName;
			}
			set
			{
				this.componentName = value;
				if(this.componentName == null )
					this.componentName = DotNetReverseEngineer.DefaultNamespace;
				if(this.componentName == string.Empty)
					this.componentName = DotNetReverseEngineer.DefaultNamespace;
			}
		}

		public string ClassSummary
		{
			get
			{

				return string.Empty;
			}
			set
			{}
		}

		public bool Abstract
		{
			get
			{
				return this.classType.IsAbstract;
			}
			set
			{}
		}

		#endregion
	}
}
